//
//  FDMDataModel.m
//  FamousDoctorOnline
//
//  Created by 刘胤辰 on 16/7/7.
//  Copyright © 2016年 wszyxc. All rights reserved.
//


#define kMatchDoctors @"http://iosapi.itcast.cn/matchDoctors.json.php"

#import "FDMDataModel.h"
#import "NetWorkTool.h"
#import <AFNetWorking.h>
@implementation FDMDataModel

+(instancetype)initwithDict:(NSDictionary *)dict{
    
    FDMDataModel *model=[[self alloc]init];
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

+(void)dataArrayWithSuccessBlock:(void(^)(NSArray <FDMDataModel*>*dataArray))successBlock errorBlock:(void(^)())errorBlock
{
    //url地址
    NSString *urlString = kMatchDoctors;
    //请求参数
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"page_size":@15,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"page":@1,
                               @"has_diagnosis":@2
                               };
    //1.创建单例工具
    NetWorkTool *tool = [NetWorkTool shareManager];
    //2.可接受格式
    tool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    //3.请求格式(默认 : 二进制)
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    //4.超时时间
    tool.requestSerializer.timeoutInterval = 15;
    //5.发送POST请求
    [tool POST:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {

        NSArray *arr = responseObject[@"data"];
        NSMutableArray *marr = [NSMutableArray array];
        //遍历 字典转模型
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            FDMDataModel *model = [FDMDataModel initwithDict:obj];
            [marr addObject:model];
        }];
        
        //block传递模型数组
        if (successBlock) {
            successBlock(marr.copy);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        if (errorBlock) {
            errorBlock();
        }
        
    }];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ , %@",[super description], _doctor_name];
}

@end
